/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MyDate;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
import sUtils.*;

/**
 *
 * @author thomas.s
 */

public class MyDateDay extends Canvas {
    
    static String monthsDE[] = { "Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez" };
    static String weekdaysDE[] = { "Sa", "So", "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So", "??" };
    static String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    static String weekdays[] = { "Sa", "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su", "??" };
    static int colors[] = { 0xa0a080, 0x80a0a0, 0xa080a0, 0x80a080, 0xa08080, 0x8080a0 };

    long days;
    public int month, day, weekday, year;
    public long timeStart, timeEnd;
    TextImage img = null;
    String text = "";
    public Vector entry = null;
    int meno=0;
    boolean newImg=false;
    int bgColor=0x808080, txtColor=0xffffff;
    MIDlet parent=null;
    Displayable oldcanvas=null;
    long lastPressed=0;
    int current;

    public MyDateDay( Calendar c, long t, boolean current, PIMReader pim )
    {
        timeStart = t;
        timeEnd = t+24*60*60000;
        days = t/(24*60*60000);
        month = c.get(Calendar.MONTH)+1;
        day = c.get(Calendar.DAY_OF_MONTH);
        weekday = c.get(Calendar.DAY_OF_WEEK);
        year = c.get(Calendar.YEAR);
        meno = pim.getMeno(t);
        setCurrent(current);
        pim.addDates(this);
    }

    public void setCurrent( boolean current )
    {
        long now = System.currentTimeMillis()-TimeZone.getDefault().getRawOffset();
        int col = colors[month%6];
        txtColor = ((now>=timeStart && now<timeEnd)||current) ? 0xffffff : col;
        bgColor = darken(col,current?160:(weekday==Calendar.SUNDAY?80:(weekday==Calendar.SATURDAY?75:40)));
        newImg = true;
    }

    public void clear()
    {
        synchronized(this)
        {
            img = null;
            entry = null;
            text = "";
        }
    }

    public void refresh()
    {
        synchronized(this)
        {
            img = null;
        }
    }

    public void addEntry( MyDateEntry ev  )
    {
        synchronized(this)
        {
            img = null;
            if (entry==null) entry = new Vector(5,1);
                        
            boolean inserted = false;
            // sorted insert:
            MyDateEntry e=null;

            for ( int i=0; i<entry.size(); i++ )
            {
               e = (MyDateEntry)entry.elementAt(i);
               if (e.time>ev.time)
               {
                    entry.insertElementAt( ev, i );
                    inserted = true;
                    break;
               }
            }
            if (!inserted) entry.addElement( ev );
            
        }
    }

    public boolean removeEntry( MyDateEntry re )
    {
        boolean rc=false;
        synchronized(this)
        {
            if (re!=null && entry!=null)
                if (rc=entry.removeElement(re))
                 img=null;
        }
        return true;
    }

    public boolean updateEntry( MyDateEntry re )
    {
        boolean rc=false;
        synchronized(this)
        {
            if (rc=entry.contains(re)) img=null;
        }
        return rc;
    }

    public int darken( int c, int v )
    {
        return ((((c&0xff00ff)*(v))&0xff00ff00) + (((c&0xff00)*v)&0xff0000))>>8;
    }

    public void paint( Graphics g, int x, int y, int w, int h, ImgRes res )
    {
        synchronized(this)
        {
            g.setColor( 0 );
            g.fillRect(x, y, w, 1);

            Font f = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN, Font.SIZE_SMALL);
            g.setFont(f);

            g.setColor( bgColor );
            g.fillRect(x, y+1, w, h-1);
            g.setColor( bgColor+0x303030);

            g.drawString(Lang.id==1 ? monthsDE[month-1] : months[month-1], x+65, y+1, 0);

            g.setColor( txtColor );
            String s;
            s = (day<10?"0":"") + day;
            g.drawString(s, x, y+(h-f.getHeight())/2+2, 0);
            s = Lang.id==1 ? weekdaysDE[weekday] : weekdays[weekday];
            g.drawString(s,x+64-f.stringWidth(s),y+(h-f.getHeight())/2+2,0);
            if (newImg || (entry!=null && img==null))
            {
               // splitText();
                img = new TextImage(entry, timeStart, h, bgColor, txtColor);
                newImg = false;
            }
            x+=85;

           try{
              if (meno==-2) g.drawImage(res.meno_female, x, y+23, 0); else
              if (meno==-1) g.drawImage(res.meno_female2, x, y+23, 0); else
              if (meno==1) g.drawImage(res.meno_baby2, x, y+23, 0); else
              if (meno==2) g.drawImage(res.meno_baby, x, y+23, 0);
           } catch( Exception e ) {}
           if (img!=null) img.draw(g, x+22, y+3);
        }
    }


    public String getDateStr()
    {
        StringBuffer sb = new StringBuffer(50);
        sb.append( Lang.id==1 ? weekdaysDE[weekday] : weekdays[weekday] ); sb.append(" ");
        if (day<10) sb.append('0'); sb.append(day); sb.append('.');
        if (month<10) sb.append('0'); sb.append(month); sb.append('.');
        sb.append(year);
        return sb.toString();
    }

    public void paint( Graphics g )
    {
        int w = getWidth();
        int h = getHeight();

        g.setColor(0);
        g.fillRect(0,0,w,h);

        Font fs = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN, Font.SIZE_SMALL);
        g.setFont(fs);
        MyDateEntry e;
        for ( int i=0; i<entry.size(); i++ )
        {
            e = (MyDateEntry)entry.elementAt(i);
            g.setColor(i==current?0xf0f080:0x8080f0);
            g.drawString( e.getDesc(timeStart), 10, 10+i*50, 0 );
            g.setColor(0x303040);
            g.fillRect(0, i*50+50, w,1);
        }
    }

    public void pointerPressed( int x, int y )
    {
        long t = System.currentTimeMillis();
      //  if (t-lastPressed<300) close();
        lastPressed = t;
        int i = y/50;
        if (i<entry.size()) { current=i; repaint(); }
    }

    public void pointerDragged( int x, int y )
    {
        int i = y/50;
        if (i<entry.size()) { current=i; repaint(); }
    }

    public void pointerReleased( int x, int y )
    {
        int i = y/50;
        if (current<entry.size() && current>=0)
        {
            MyDateEntry e;
            e = (MyDateEntry)entry.elementAt(current);
        }
    }

    public MyDateEntry getCurrent()
    {
        return (current>=0 && current<entry.size()) ? (MyDateEntry)entry.elementAt(current) : null;
    }

    public void keyPressed( int key )
    {
        close();
    }

    public void show( MIDlet p )
    {
        parent = p;
        current = -1;

        oldcanvas = Display.getDisplay(p).getCurrent();
        setTitle("MyDate - " + getDateStr());
        Display.getDisplay(p).setCurrent(this);
    }

    public void close()
    {
        Display.getDisplay(parent).setCurrent(oldcanvas);
        oldcanvas=null;
        parent=null;
    }


}
